package com.enjoy.xbase.db;

import android.database.sqlite.SQLiteDatabase;

import com.enjoy.xbase.db.annotation.AnnTableName;
import com.enjoy.xbase.db.handle.IDelete;
import com.enjoy.xbase.db.handle.IInsert;
import com.enjoy.xbase.db.handle.ISelect;
import com.enjoy.xbase.db.handle.ISelectSql;
import com.enjoy.xbase.db.handle.IUpdate;
import com.enjoy.xbase.db.impi.MDelete;
import com.enjoy.xbase.db.impi.MInsert;
import com.enjoy.xbase.db.impi.MSelect;
import com.enjoy.xbase.db.impi.MSelectSql;
import com.enjoy.xbase.db.impi.MUpdate;
import com.enjoy.xbase.db.tools.DBTools;

import java.io.File;

/**
 * 描述：数据库操作
 */
public class DbManger {
    private static DbManger instance;
    private Configure configure;
    private SQLiteDatabase db;

    /**
     * 描述：数据库初始化
     * @param version 版本
     * @param dbName
     * @param dbPath
     * @param clazs
     */
    public static void init(int version, String dbName, String dbPath, Class... clazs) {
        instance = new DbManger();
        instance.configure = new Configure(version, dbName, dbPath, clazs);
    }

    private DbManger(){

    }

    /**
     * 描述：初始化数据库
     */
    private void initDb() {
        if (configure == null) {
            throw new RuntimeException("使用前请配置数据库初始化");
        }
        File file = new File(configure.dbPath);
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }
        SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(configure.dbPath, null);
        if (db.getVersion() < configure.version) {
            DBTools.update(db,configure.clazs);
            db.setVersion(configure.version);
        }
        this.db = db;
    }
    public synchronized static DbManger getInstance() {
        if (instance == null) {
            throw new RuntimeException("使用前请配置数据库初始化");
        }
        return instance;
    }

    public SQLiteDatabase getSQLiteDatabase() {
        if (db == null) {
            try {
                initDb();
            } catch (Exception e) {
                db = null;
            }
        }
        return db;
    }



    /**
     * 描述：数据库是否创建成功
     * @return
     */
    public boolean isCreate(){
        return db!=null;
    }

    /**
     * 获取插入数据
     * @return
     */
    public IInsert createInsert() {
        SQLiteDatabase db = getSQLiteDatabase();
        if (db == null) {
            return null;
        } else {
            return new MInsert(db);
        }
    }


    /**
     * 描述：创建更新操作
     * @param tabName
     * @return
     */
    public IUpdate createUpdate(String tabName) {
        SQLiteDatabase db = getSQLiteDatabase();
        if (db == null) {
            return null;
        } else {
            return new MUpdate(db,tabName);
        }
    }
    /**
     * 描述：创建更新操作
     * @param clazz
     * @return
     */
    public IUpdate createUpdate(Class clazz) {
        SQLiteDatabase db = getSQLiteDatabase();
        if (db == null) {
            return null;
        } else {
            return new MUpdate(db, DBTools.getTabName(clazz));
        }
    }

    /**
     * 描述：创建删除类
     * @return
     */
    public IDelete createDelete() {
        SQLiteDatabase db = getSQLiteDatabase();
        if (db == null) {
            return null;
        } else {
            return new MDelete(db);
        }
    }
    /**
     * 描述：创建删除类
     * @return
     */
    public IDelete createDelete(String tbName) {
        SQLiteDatabase db = getSQLiteDatabase();
        if (db == null) {
            return null;
        } else {
            return new MDelete(db, tbName);
        }
    }
    /**
     * 描述：创建删除类
     * @return
     */
    public IDelete createDelete(Class clazz) {
        SQLiteDatabase db = getSQLiteDatabase();
        if (db == null) {
            return null;
        } else {
            return new MDelete(db, DBTools.getTabName(clazz));
        }
    }

    /**
     * 描述：创建数据查询类
     * @return
     */
    public  ISelect createSelect() {
        SQLiteDatabase db = getSQLiteDatabase();
        if (db == null) {
            return null;
        } else {
            return new MSelect(db);
        }
    }
    /**
     * 描述：创建数据查询类
     * @return
     */
    public  ISelectSql createSelectSql() {
        SQLiteDatabase db = getSQLiteDatabase();
        if (db == null) {
            return null;
        } else {
            return new MSelectSql(db);
        }
    }

    /**
     * 数据库配置类型
     */
    public static class Configure{
        public int version;
        public String dbName;
        public String dbPath;
        public Class[] clazs;

        public Configure(int version, String dbName, String dbPath, Class[] clazs) {
            this.version = version;
            this.dbName = dbName;
            this.dbPath = dbPath;
            this.clazs = clazs;
        }
    }
}
